import { Message } from 'element-ui';

export async function selectDir() {
  try {
    const handle = await window.showDirectoryPicker()
    return await processHandle(handle)
  } catch(e) {
    if (e.name == 'TypeError') {
      Message.error("使用了ip或者非https的方式访问，无该方法的权限")
    } else {
      console.log(e, "拒绝读取文件夹")
    }
  }
}

async function processHandle(handle) {
  if (handle.kind == "file") {
    return handle
  }
  handle.children = []
  const iter = handle.entries()
  for await (const item of iter) {
    handle.children.push(await processHandle(item[1]))
  }
  return handle
}